<template>
  <div class="order-list-container">
    <div class="header-container">
      <h2>我的订单</h2>
      <div class="filter-bar">
        <button @click="changeStatus(null)">全部</button>
        <button @click="changeStatus(1)">待付款</button>
        <button @click="changeStatus(3)">待发货</button>
        <button @click="changeStatus(4)">已发货</button>
        <button @click="changeStatus(5)">已取消</button>
      </div>
    </div>
    <!-- 订单列表 -->
    <div v-if="orders.length > 0" class="orders">
      <div v-for="order in orders" :key="order.id" class="order-card">
        <div class="order-header">
          <span>订单号：{{ order.number }}</span>
          <span class="status-tag" :class="statusClass(order.status)">
            {{ statusText(order.status) }}
          </span>
        </div>

        <!-- 商品信息 -->
        <div class="order-items">
          <div v-for="item in order.orderDetailList" :key="item.id" class="item-row">
            <img :src="item.image || 'https://via.placeholder.com/60x60'" alt="商品图片" class="item-image" />
            <div class="item-info">
              <p class="item-name">{{ item.name }}</p>
              <p class="item-quantity">数量：{{ item.number }}</p>
              <p class="item-price">单价：￥{{ item.amount }}</p>
            </div>
          </div>
        </div>

        <!-- 订单信息 -->
        <div class="order-footer">
          <p><strong>收货人：</strong>{{ order.consignee }}</p>
          <p><strong>地址：</strong>{{ order.address }}</p>
          <p class="order-time"><strong>下单时间：</strong>{{ order.orderTime }}</p>
          <p><strong>总金额：</strong><span class="total-amount">￥{{ order.amount }}</span></p>

          <!-- 操作按钮 -->
          <div class="order-actions">
            <el-button v-if="order.status === 1" type="danger" size="small" @click="handleCancelOrder(order.id)">
              取消订单
            </el-button>
            <el-button v-if="order.status === 1" type="primary" size="small" @click="gotoPayment(order)">
              立即支付
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <div v-else class="no-orders">
      <p>暂无订单</p>
    </div>
    <el-pagination
        v-model:total="total"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChange"
        @current-change="loadOrders"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import {cancelOrder, getHistoryOrders} from '@/apis/orders.js'
import {ElMessage, ElMessageBox} from 'element-plus'

const changeStatus = (status) => {
  queryParams.status = status
  loadOrders(1)
}

const queryParams = reactive({
  status: null,
  page: 1,
  pageSize: 5
})

const orders = ref([])
const total = ref(0)

const loadOrders = async (page = 1) => {
  queryParams.page = page
  try {
    const res = await getHistoryOrders(queryParams)
    if (res.code === 0) {
      orders.value = res.data.records || []
      total.value = res.data.total
    } else {
      ElMessage.error(res.message || '获取订单失败')
    }
  } catch (error) {
    console.error('获取订单失败:', error)
    ElMessage.error('网络异常，请稍后再试')
  }
}

const statusText = (status) => {
  switch (status) {
    case 1: return '待付款'
    case 3: return '待发货'
    case 4: return '已发货'
    case 5: return '已取消'
    default: return '未知状态'
  }
}

const statusClass = (status) => {
  switch (status) {
    case 1: return 'pending'
    case 3: return 'paid'
    case 4: return 'shipped'
    case 5: return 'canceled'
    default: return ''
  }
}

const pageSizeChange = (size) => {
  queryParams.pageSize = size
  loadOrders(1)
}

const gotoPayment = (order) => {
  router.push({
    path: '/payment',
    query: {
      number: order.number,
      amount: order.amount,
      createTime: order.orderTime
    }
  })
}

const handleCancelOrder = async (id) => {
  try {
    await ElMessageBox.confirm('确定要取消该订单吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })

    const res = await cancelOrder(id)
    if (res.code === 0) {
      ElMessage.success('订单取消成功')
      loadOrders()
    } else {
      ElMessage.error(res.message || '取消失败')
    }
  } catch (error) {
    console.error('取消订单失败:', error)
    ElMessage.error('操作失败，请重试')
  }
}

onMounted(() => {
  loadOrders()
})
</script>

<style scoped lang="scss">
$primary-color: #2c3e50;
$accent-color: #e74c3c;
$success-color: #27ae60;
$info-color: #3498db;
$warning-color: #f1c40f;
$background-color: #f4f7fa;
$card-bg: #ffffff;
$border-radius: 12px;
$shadow: 0 4px 20px rgba(0, 0, 0, 0.08);

.order-list-container {
  max-width: 1000px;
  margin: 40px auto;
  padding: 30px;
  background: linear-gradient(145deg, $background-color, #e8edf3);
  border-radius: $border-radius;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  box-shadow: $shadow;
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32px;

  h2 {
    font-size: 28px;
    font-weight: 700;
    color: $primary-color;
    margin: 0;
    letter-spacing: -0.5px;
  }
}

.filter-bar {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;

  button {
    padding: 10px 24px;
    border: none;
    background: #ffffff;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    color: $primary-color;
    transition: all 0.3s ease;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    position: relative;
    overflow: hidden;

    &:hover {
      background: linear-gradient(90deg, #3498db, #2980b9);
      color: white;
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    }

    &:active {
      transform: translateY(0);
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }

    &:focus {
      outline: none;
      box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.3);
    }

    &.active {
      background: linear-gradient(90deg, #3498db, #2980b9);
      color: white;
    }
  }
}

.order-card {
  background: $card-bg;
  border-radius: $border-radius;
  margin-bottom: 24px;
  padding: 24px;
  box-shadow: $shadow;
  transition: transform 0.3s ease;

  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
  }

  .order-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-bottom: 16px;
    border-bottom: 1px solid #e8ecef;

    span:first-child {
      font-size: 16px;
      font-weight: 600;
      color: $primary-color;
    }

    .status-tag {
      padding: 6px 16px;
      font-size: 13px;
      font-weight: 600;
      color: white;
      border-radius: 20px;
      text-transform: uppercase;
      letter-spacing: 0.5px;

      &.pending { background: linear-gradient(90deg, $warning-color, #e67 est-ce que22); }
      &.paid { background: linear-gradient(90deg, $success-color, #2ecc71); }
      &.shipped { background: linear-gradient(90deg, $info-color, #2980b9); }
      &.canceled { background: linear-gradient(90deg, $accent-color, #c0392b); }
    }
  }

  .order-items {
    display: flex;
    flex-direction: column;
    gap: 16px;
    margin-bottom: 20px;
  }

  .item-row {
    display: flex;
    align-items: center;
    padding: 12px;
    background: #f8fafc;
    border-radius: 8px;
    transition: background 0.2s ease;

    &:hover {
      background: #f1f5f9;
    }
  }

  .item-image {
    width: 80px;
    height: 80px;
    object-fit: cover;
    border-radius: 8px;
    margin-right: 16px;
    border: 1px solid #e8ecef;
  }

  .item-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-size: 14px;
    color: $primary-color;

    .item-name {
      font-weight: 600;
      margin: 0;
    }

    .item-quantity, .item-price {
      color: #6b7280;
      margin: 0;
    }
  }

  .order-footer {
    border-top: 1px solid #e8ecef;
    padding-top: 20px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    position: relative;

    p {
      margin: 0;
      font-size: 14px;
      color: $primary-color;

      strong {
        font-weight: 600;
      }
    }

    .order-time {
      grid-column: 2;
      text-align: right;
      font-size: 13px;
      color: #6b7280;
    }

    .total-amount {
      color: $accent-color;
      font-size: 18px;
      font-weight: 700;
    }

    .order-actions {
      grid-column: 2;
      text-align: right;
      margin-top: 12px;

      .el-button {
        border-radius: 8px;
        font-weight: 600;
        transition: all 0.3s ease;

        &--primary {
          background: linear-gradient(90deg, #3498db, #2980b9);
          border: none;

          &:hover {
            background: linear-gradient(90deg, #2980b9, #1f618d);
          }
        }

        &--danger {
          background: linear-gradient(90deg, $accent-color, #c0392b);
          border: none;

          &:hover {
            background: linear-gradient(90deg, #c0392b, #a93226);
          }
        }
      }
    }
  }
}

.no-orders {
  text-align: center;
  color: #6b7280;
  margin: 60px 0;
  font-size: 18px;
  font-weight: 500;
}

.el-pagination {
  margin-top: 24px;
  justify-content: center;

  :deep(.el-pager li) {
    border-radius: 8px;
    margin: 0 4px;
    transition: all 0.3s ease;

    &.is-active {
      background: linear-gradient(90deg, #3498db, #2980b9);
      color: white;
    }

    &:hover {
      background: #e8ecef;
    }
  }

  :deep(.el-pagination__sizes .el-input__inner) {
    border-radius: 8px;
  }
}
</style>